package com.victor.utils.sms;

import java.util.Date;
import java.util.TreeMap;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

/**
 * 帐户查询接口
 * 
 * @author v
 *
 */
public class Query {

	private static Logger log = LoggerFactory.getLogger(BuildTemplate.class);

	private static String KEY = "fa604ba76d4dad3c";

	public static void main(String[] args) {
		// 创建模板
		query();
	}

	/**
	 * 创建模板
	 */
	public static void query() {
		try {
			TreeMap<String, Object> map = new TreeMap<String, Object>();
			map.put("cpid", 2907);
//			map.put("name", "会员短信模板");
//			map.put("sign", "云科智慧游");
//			map.put("content", "预约的项目还有10分钟就到你啦！请尽快前往目的地。排队实时信息，请在APP内查阅。");

			map.put("timestamp", new Date().getTime());

			String _sign = SmsUseful.createSign(map); 
			System.out.println("sign:" + _sign);
			String sign = SmsUseful.getKeyedDigestGBK(_sign, KEY);
			System.out.println(sign);

			map.put("verify", sign);

			String resource = "http://api.hstang.net/service/account/query";

			CloseableHttpClient httpClient = HttpClientBuilder.create().build();

			String body = JSON.toJSONString(map);
			log.info(body);
			HttpPost post = new HttpPost(resource);
			StringEntity se = new StringEntity(body, "UTF-8");
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
			post.addHeader("Content-Type", "application/json;charset=UTF-8");
			post.addHeader("accept", "application/json");
			post.setEntity(se);
			HttpResponse httpResponse = httpClient.execute(post);
			log.info(String.valueOf(httpResponse.getStatusLine().getStatusCode()));
			log.info(IOUtils.toString(httpResponse.getEntity().getContent()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
}
